package com.dsbj.mp.icardservice.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.Dept;
import com.dsbj.mp.icardservice.service.DeptService;
import com.dsbj.mp.icardservice.utils.strprocess.ObjectParse;
import com.dsbj.mp.icardservice.utils.strprocess.StrUtil;
import com.dsbj.mp.icardservice.webserviceclient.dsbjpmc.AllowUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author cjw 2019-09-23
 */
@RestController
@RequestMapping(value = "/dept")
public class DeptController {

	private Logger logger = LoggerFactory.getLogger(getClass());


	@Resource
	private DeptService deptService;



	/**
	 * 根据deptname模糊查询dept信息。
	 * <p>
	 *     tip: deptname中亦可传入deptno
	 *
	 * </p>
	 * <br>传入参数 deptname, companyno, allowUser(allowUser和companyno，二选一即可，优先取companyno)。
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	@RequestMapping(value = "/getDeptByNameDim",method = RequestMethod.POST)
	public ResponseMessage getDeptByNameDim(@RequestBody Map<String,Object> map){

		//解析传来的参数
		AllowUser allowUser=null;
		String deptname=null;
		String companyno=null;
		try{
			//解析allowUser
			if(!StrUtil.isEmpty(map.get("allowUser"))){
				Object object=map.get("allowUser");
				String objectStr=JSON.toJSONString(object, SerializerFeature.WriteMapNullValue);
				allowUser=JSON.parseObject(objectStr,new TypeReference<AllowUser>(){}, Feature.OrderedField);
			}
			//解析companyno
			companyno=ObjectParse.parse_toStr(map.get("companyno"));
			//解析dept
			deptname= ObjectParse.parse_toStr_elseException(map.get("deptname"));

			/**
			 * 更新companyno中的信息
			 */
			if(StrUtil.isEmpty(companyno)){
				if(!StrUtil.isEmpty(allowUser)&&!StrUtil.isEmpty(allowUser.getCompanyno())){
					companyno=allowUser.getCompanyno();
				}else{
					throw new RuntimeException("请求失败, 需传入companyno!");
				}
			}

		}catch (Exception e){
			return ResponseMessage.error("传入的参数有误!");
		}

		/**
		 * 根据deptName模糊查询部门信息。
		 */
		List<Dept> list_dept=null;
		list_dept= deptService.getDeptByNameDim(companyno,deptname);
		return ResponseMessage.ok(list_dept);
	}

















}
